<?php
namespace app\common\model;

use app\api\model\ProductOrder;
use think\facade\Log as SystemLog;
use think\Model;


class ProductOrderSpell extends Model
{
    /**
     * 根据订单号完成拼团
     * @param $order_id
     * @param $user_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function trueOrderSpell($order_id,$user_id){
        SystemLog::record("[回调拼团处理]当前订单进入拼团处理接口", 'alert');

        //查询本人团信息
        $where["order_id"] = $order_id;
        $where["user_id"] = $user_id;
        $model = $this->where($where)->find();
        SystemLog::record($model);
        if($model == null){
            SystemLog::record("[回调拼团处理]当前订单拼团不存在", 'alert');
            return false;
        }

        //查询当前团的信息
        $productSpellService = new ProductSpell();
        $spellModel = $productSpellService->where(["id"=>$model["product_spell_id"]])->find();
        SystemLog::record($spellModel);
        if($spellModel == null){
            SystemLog::record("[回调拼团处理]当前订单拼团商品信息不存在", 'alert');
            return false;
        }

        //增加拼团商品销量
        $res = $productSpellService->where(["id"=>$model["product_spell_id"]])->setInc("sales_num",$spellModel["batch_num"]);
        if(!$res){
            SystemLog::record("[回调拼团处理]当前订单拼团商品销量处理失败", 'alert');
            return false;
        }

        //判断当前团自己是否为团长
        if($model["parent_id"] == 0){
            //是团长则只需要修改自己的状态为1即可
            $res = $this->where($where)->setField("status","1");
            if(!$res){
                SystemLog::record("[回调拼团处理]当前订单拼团状态修改失败_A", 'alert');
                return false;
            }
        }
        else{
            //查询当前团内有多少人
            $countWhere["parent_id"] = $model["parent_id"];
            $countWhere["status"] = 1;
            $count = $this->where($countWhere)->whereNull("om_product_order_spell.delete_time")->count();

            if($spellModel["spell_num"] - $count - 1 > 0){
                //有剩余位置 只修改自己的 是团长则只需要修改自己的状态为1 拼团中
                $res = $this->where($where)->setField("status","1");
                if(!$res){
                    SystemLog::record("[回调拼团处理]当前订单拼团状态修改失败_B", 'alert');
                    return false;
                }
            }else{
                //无剩余位置 修改自己的和团长则修改为2已完成
                //更改团长单
                $res = $this->where(["id"=>$model["parent_id"]])->setField("status","2");
                if(!$res){
                    SystemLog::record("[回调拼团处理]当前订单拼团状态修改失败_C", 'alert');
                    return false;
                }
                //更改父级相同的所有单
                $res = $this->where(["parent_id"=>$model["parent_id"]])->setField("status","2");
                if(!$res){
                    SystemLog::record("[回调拼团处理]当前订单拼团状态修改失败_D", 'alert');
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 获取当前订单下的拼团情况
     * @param $order_id
     * @return array|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOrderSpell($order_id){
        //查询当前order_id 对应的团单情况
//        select phone,user_id,order_id,parent_id,om_product_order_spell.id,om_product_order_spell.status from om_product_order_spell
//        inner join om_user on om_product_order_spell.user_id = om_user.id
//        where order_id = 971
        $field = 'phone,user_id,order_id,parent_id,om_product_order_spell.id,om_product_order_spell.status,product_spell_id';
        $spellOrderModel = $this->field($field)
            ->join("om_user","om_product_order_spell.user_id = om_user.id")
            ->whereNull("om_product_order_spell.delete_time")
            ->where(["order_id"=>$order_id]) ->find();
        if($spellOrderModel == null){ return null; }

        //获取当前团成团信息
        $productSpellService = new ProductSpell();
        $spellModel = $productSpellService->where(["id"=>$spellOrderModel["product_spell_id"]])->find();
        if($spellModel == null){ return null; }

        //判断当前parent_id
        if($spellOrderModel["parent_id"] == 0){
            $list = $this->field($field)
                ->where("parent_id","eq",$spellOrderModel["id"])
                ->where("om_product_order_spell.status","in",[1,2])
                ->join("om_user","om_product_order_spell.user_id = om_user.id")
                ->whereNull("om_product_order_spell.delete_time")
                ->select()->toArray();
            array_push($list,$spellOrderModel->toArray());
        }
        else{
            $list = $this->field($field)
                ->where("parent_id","eq",$spellOrderModel["id"])
                ->where("om_product_order_spell.status","in",[1,2])
                ->join("om_user","om_product_order_spell.user_id = om_user.id")
                ->whereNull("om_product_order_spell.delete_time")
                ->select()->toArray();
            $model = $this->field($field)
                ->where("om_product_order_spell.id","eq",$spellOrderModel["parent_id"])
                ->where("om_product_order_spell.status","in",[1,2])
                ->whereNull("om_product_order_spell.delete_time")
                ->join("om_user","om_product_order_spell.user_id = om_user.id")
                ->find();
            if($model == null){ return null; }
            array_push($list,$model);
        }

        $result["is_true"] = $spellOrderModel["status"] == 2 ? 1 : 0;
        $result["spell_num"] = $spellModel["spell_num"];
        $result["now_num"] = Count($list);
        $result["data"] = $list;
        return $result;
    }

    /**
     * 取消拼团
     * @param $order_id
     * @return bool
     */
    public function falseOrderSpell($order_id){
        SystemLog::record("[取消拼团处理]当前订单".$order_id."拼团状态取消", 'alert');
        $res = $this->where(["order_id"=>$order_id])->setField("status",-1);
        if(!$res){
            return false;
        }
        return true;
    }
}